import { RouteRecordRaw } from 'vue-router'
import { IBreadcrumb } from '@/base-ui/breadcrumb'
//定义变量保存菜单
let firstMenu: any = null
//参数为外界传入的菜单值
export function mapMenusToRouter(userMenus: any[]): RouteRecordRaw[] {
  //对应的路由信息
  const routes: RouteRecordRaw[] = []

  //1.先默认加载所有的routes
  const allRouter: RouteRecordRaw[] = []
  //获main文件中所有ts路径        context是webpack中的函数，第一个参数为路径，第二个为是否进行递归，第三个是正则
  const routerFiles = require.context('../router/main', true, /\.ts/)
  //通过key()把routerFiles进行解析，然后遍历里面的每一项
  routerFiles.keys().forEach((key) => {
    //通过require拿到了对应文件里面内容，使用split把路径给分割获取url
    const route = require('../router/main' + key.split('.')[1])
    //把每一项的值保存在allRouter中   数据保存在default中
    allRouter.push(route.default)
  })
  // console.log(allRouter)

  //2.根据菜单传来的url路径与所有的routes进行判断，如果相等则添加到router数组中
  //定义一个函数，把菜单数据当作参数传入，，把里面的每一项进行遍历判断allRouter里面的路径是否和传入的路径相同
  //如果相同，把路径添加到routes中
  //还需要根据菜单的type进行判断，如果type === 2直接进行判断，如果type === 1因为1中是没路径的，所有对要把里面的children当作参数传入重新进行遍历
  const _recurseGetRouter = (menus: any[]) => {
    for (const menu of menus) {
      //判断type===2
      if (menu.type === 2) {
        //如果我们获取路径里面的值的path等于外界传来的url那么我们把这个值返回出去
        const route = allRouter.find((route) => route.path === menu.url)
        if (route) routes.push(route)
        //判断如果firstMenu没有值时，我们把菜单保存
        if (!firstMenu) {
          firstMenu = menu
        }
      } else {
        _recurseGetRouter(menu.children)
      }
    }
  }
  //把userMenus当作参数传入
  _recurseGetRouter(userMenus)
  //把路由信息返回，实现动态的添加
  return routes
}
export function pathMapBreadcrumbs(userMenus: any[], currentPath: string) {
  const breadcrumbs: IBreadcrumb[] = []
  pathMapToMenu(userMenus, currentPath, breadcrumbs)
  return breadcrumbs
}

export function pathMapToMenu(
  userMenus: any[],
  currentPath: string,
  breadcrumbs?: IBreadcrumb[]
): any {
  for (const menu of userMenus) {
    if (menu.type === 1) {
      const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
      if (findMenu) {
        breadcrumbs?.push({ name: menu.name })
        breadcrumbs?.push({ name: findMenu.name })
        return findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      return menu
    }
  }
}
export function mapMenusToPermissions(userMenus: any[]) {
  //获取type===3时的路径可以判断有没有权限
  const permissions: string[] = []
  //使用递归进行遍历判断更严谨点，
  const _recurseGetPermission = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 1 || menu.type === 2) {
        _recurseGetPermission(menu.children ?? [])
      } else if (menu.type === 3) {
        permissions.push(menu.permission)
      }
    }
  }
  _recurseGetPermission(userMenus)

  return permissions
}

export function menuMapLeafKeys(menuList: any[]) {
  const leftKeys: number[] = []

  const _recurseGetLeaf = (menuList: any[]) => {
    for (const menu of menuList) {
      if (menu.children) {
        _recurseGetLeaf(menu.children)
      } else {
        leftKeys.push(menu.id)
      }
    }
  }
  _recurseGetLeaf(menuList)

  return leftKeys
}

export { firstMenu }
